package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_address;

import cn.com.api.service.ApiT_addressService;
import cn.com.backstage.controller.base.T_addressBaseController;

import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE+"/")
public class T_addressController  extends T_addressBaseController {

	private static Logger logger = Logger.getLogger(T_addressController.class);
	
	@Autowired
	private ApiT_addressService apiT_addressService;
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping(value = "uid_address-list")
	public ModelAndView uid_address_list(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_address_list.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
/*			List<ApiT_address> t_addresss = apiT_addressService.findT_addresssByU_id(id);
			mv.addObject("t_addresss", t_addresss);*/
		}
		return mv;
	}
	
	/**
	 * 进入新增或者编辑页面
	 * @return
	 */
	@RequestMapping(value = "address-add-pre")
	public ModelAndView address_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView mv = new ModelAndView("forward:/backstage/t_address_add.jsp");
		//
		if(request.getParameter("id")!=null){
			String id = request.getParameter("id");
			ApiT_address t_address = apiT_addressService.findT_addressById(Integer.valueOf(id));
			mv.addObject("t_address",t_address);
		}
		return mv;
	}

	/**
	 *新增或者编辑
	 * @return
	 */
	@RequestMapping(value = "address-add")
	public void address_add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		String u_id = request.getParameter("u_id");
		String province = request.getParameter("province");
		String city = request.getParameter("city");
		String county = request.getParameter("county");
		String address = request.getParameter("address");
		String longitude = request.getParameter("longitude");
		String latitude = request.getParameter("latitude");
		String phone = request.getParameter("phone");
		String name = request.getParameter("name");
		String enable = request.getParameter("enable");
		
		Map<String,Object> map = new HashMap<String,Object>();
		String id = request.getParameter("id");
		//add
		if(id==null || (id.trim()).equals("")){
			ApiT_address t_address = new ApiT_address();
			/*
		    //t_address.setId(id);
		    t_address.setU_id(u_id);
		    t_address.setProvince(province);
		    t_address.setCity(city);
		    t_address.setCounty(county);
		    t_address.setAddress(address);
		    t_address.setLongitude(longitude);
		    t_address.setLatitude(latitude);
		    t_address.setPhone(phone);
		    t_address.setName(name);
		    t_address.setEnable(enable);
	        */
			apiT_addressService.insertT_address(t_address);
			//
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "添加成功！");
		}
		//edit
		else{
			ApiT_address t_address = apiT_addressService.findT_addressById(Integer.valueOf(id));
	      
		    //t_address.setId(id);
		    //t_address.setU_id(u_id);
		    //t_address.setProvince(province);
		    t_address.setCity(city);
		    //t_address.setCounty(county);
		    t_address.setAddress(address);
		    t_address.setLongitude(longitude);
		    t_address.setLatitude(latitude);
		    //t_address.setPhone(phone);
		    //t_address.setName(name);
		    //t_address.setEnable(enable);
			
			apiT_addressService.updateT_address(t_address);
			//
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		}

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}
	
}



